<html>
<head>
<script src="js/highcharts.js"></script>

<script type="text/javascript">
	var aJaxURL	= "server-side/report/sales_statistics.action.php";		  //server side folder url
	var seoyURL = "server-side/seoy/seoy.action.php";					  //server side folder url
	var url     = "server-side/report/sale_statistics/get_sale_sum.php"; 



	$(document).ready(function() {
		GetDate("search_start");
		GetDate("search_end");
	
		$("#search_start").val(GetDateTime(2));
		$("#search_end").val(GetDateTime(2));
	
	
		var start	= $("#search_start").val();
		var end		= $("#search_end").val();

		SeoY("prod_name", seoyURL, "sales_statistics", "", 0);
			
		getData(start, end, prod_name);

		GetSaleSum(start,end);
	});
	
	$(document).on("change", "#search_start", function () {
		var start	= $(this).val();
		var end		= $("#search_end").val();
		var prod_name = $("#prod_name").val();
		
		getData(start, end, prod_name);

		if(prod_name == '' || prod_name == 'საერთო ნავაჭრი'){
			GetSaleSum(start,end);
		}else{
			GetProductionSum(prod_name, start, end);
		}

	});
	
	$(document).on("change", "#search_end", function () {
		var start	= $("#search_start").val();
		var end		= $(this).val();
		var prod_name = $("#prod_name").val();
		
		getData(start, end, prod_name);

		if(prod_name == '' || prod_name == 'საერთო ნავაჭრი'){
			GetSaleSum(start,end);
		}else{
			GetProductionSum(prod_name, start, end);
		}
		
	});

	//--SeoY
    $(document).on("click", ".combobox", function (event) {
    	var i = $(this).text();
		$("#" + i).autocomplete( "search", "" );
	});
	
	function getData(start, end, prod_name){
		 var options = {
			        chart: {
			            renderTo: 'chart_container',
			            //type: 'line',
			            margin: [ 50, 50, 100, 80]
			        },
			        title: {
			            text: 'გაყიდვების სტატისტიკა',
			            x: -20 //center
			        },
			        subtitle: {
			            text: '',
			            x: -20
			        },
			        xAxis: {
			            categories: [],
			            labels: {
			                rotation: -45,
			                align: 'right',
			                
			            }
			        },
			        yAxis: {
			            title: {
			                text: 'რაოდენობა'
			            },
			            plotLines: [{
			                value: 0,
			                width: 1,
			                color: '#808080'
			            }]
			        },
			        tooltip: {
			        	//valueSuffix: ' ცალი'
			                
			        },
			        legend: {
		                layout: 'vertical',
		                align: 'right',
		                verticalAlign: 'top',
		                borderWidth: 0
			        },
			        series: []
			    }
	
			    $.getJSON("server-side/report/sales_statistics.action.php?start="+start + "&end=" + end + "&prod_name=" + prod_name, function(json) {
			    	options.xAxis.categories = json[0]['date'];
			    	options.tooltip.valueSuffix = json[0]['unit'];
			    	options.series[0] = {};
			    	options.series[0].name = json[0]['name'];
			    	options.series[0].data = json[0]['quantity'];
			    	options.series[0].type = "column";
			    	
			    	options.series[1] = {};
			    	options.series[1].name = json[0]['name'];
			    	options.series[1].data = json[0]['quantity'];
			    	options.series[1].type = "line";
			        chart = new Highcharts.Chart(options);
			    });
	}

	$(document).on("keypress", "#prod_name", function(e) {

		if(e.which == 13){		
			var prod_name = this.value;
			var start	  = $("#search_start").val();
			var end		  = $("#search_end").val();

			getData(start, end, prod_name);

			if(prod_name == '' || prod_name == 'საერთო ნავაჭრი'){
				GetSaleSum(start,end);
			}else{
				GetProductionSum(prod_name, start, end);
			}
		}
	});

	 function GetSaleSum(start,end){

      	 $("#total_money").text("იტვირთება...");
      	 
		 var param = new Object();
		 param.act ="get_sale_sum";
		 param.start= start;
		 param.end = end;
		 	 
		 $.ajax({
				url: url,
				data: param,
				success: function(data) {
					if (typeof (data.error) != "undefined") {
                  if (data.error != "") {
                      alert(data.error);
                  } else { 
                  	 $("#total_money").text(data.page);
                  }
              }
				}
			});
	 }

	 function GetProductionSum(name, start, end){
		 
		 var param = new Object();
		 var nm = name.toString();
		 param.act ="get_products_sum";
		 param.name = nm;
		 param.start = start;
		 param.end = end;
		 
			$.ajax({
				url: url,
				data: param,
				success: function(data) {
					if (typeof (data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        } else { 
                        	$("#total_money").text(data.page);
                        }
                    }
				}
			});
	 }

	</script>
	</head>
	<body>

      <div id="dt_example" class="ex_highlight_row">
       	 <div id="container" style="width:90%">
            <div id="dynamic">

            
             <div id="button_area" style="margin: 3% 0 0 0">
             <div class="seoy-row" id="prod_name_seoy" style="width: 195px;">
					<input type="text" id="prod_name" class="inpt"  placeholder="პროდუქტის სახელი" />
					<button id="prod_name_btn" class="combobox" style=" height: 25px; margin-top: 2px;">prod_name</button>
			</div>
			</div>
	       <div id="button_area" style="margin: 3% 0 0 0">
	         <div class="left" style="width: 175px;">
	           <input type="text" name="search_start" id="search_start" class="inpt right"/>
	             </div>
	            	<label for="search_start" class="left" style="margin:5px 0 0 3px">-დან</label>
	             <div class="left" style="width: 185px;">
		            <input type="text" name="search_end" id="search_end" class="inpt right" />
	             </div>
	            	<label for="search_end" class="left" style="margin:5px 0 0 3px">–მდე</label>
	            	<label class="left" style="margin:5px 0 0 80px">ჯამური თანხა: </label> <label id="total_money" class="left" style="margin:5px 0 0 2px; font-weight: bold;"></label>
	       <br /><br /><br />
	            </div>
			<div id="chart_container" style="min-width: 100%; height: 400px; margin-top:-30px;"></div>
</div>
</div>
</div>
	</body>
	</html>
